<template>
  <div>
    <el-row>
      <el-col>
        <!-- 年 -->
        <el-select
          v-model="selectYear"
          size="mini"
          placeholder="请选择"
          @change="updateCalender"
        >
          <el-option
            v-for="item in years"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <!-- 月 -->
        <el-select
          v-model="selectMonth"
          size="mini"
          placeholder="请选择"
          @change="updateCalender"
        >
          <el-option
            v-for="item in 12"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-col>
    </el-row>

    <!-- 日历 -->
    <el-calendar v-model="currentDate">
      <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
      <template
        v-slot:dateCell="{date, data}"
      >
        <div class="date-content">
          <p :class="data.isSelected ? 'is-selected' : ''">
            <span class="text">{{ data.day.split('-')[2] }}</span>
            <span v-if="isWeek(date)" class="rest">休</span>
          </p>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      years: ['2020', '2021', '2022', '2023'],
      selectYear: '2021',
      selectMonth: '1',
      currentDate: new Date()
    }
  },

  created() {
    const date = new Date()
    this.selectYear = date.getFullYear()
    this.selectMonth = date.getMonth() + 1
    this.years = Array.from(Array(11), (value, index) => (this.selectYear - 5 + index))
  },
  methods: {
    // 判断休息日
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    },

    // 更改日期
    updateCalender() {
      const date = this.selectYear + '-' + this.selectMonth
      this.currentDate = new Date(date)
    }
  }
}
</script>

<style scoped lang='scss'>
.el-row .el-col {
  display: flex;
  justify-content: flex-end;
  .el-select {
    width: 80px;
    margin: 0 3px;
  }
}

 ::v-deep .el-calendar-day {
  height:  auto;
  text-align: center;
  line-height: 30px;
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}

::v-deep thead {
  th {
    font-size: 14px;
    font-weight: bold;
  }
}

 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
 .is-selected {
    color: #1989FA;
  }
</style>
